<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;

class ShopProductUpStatusAction extends RowAction
{
    public $name = '上架';

    public function handle(AdminShopProductModel $model): Response
    {
        if (empty($model->specs->toArray())) return $this->response()->error('无规格商品不可上架')->refresh();
        $model->product_status = 2;
        $model->product_online_time = now()->timestamp;
        $model->operated_at = now();
        $model->publish_at = now();
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.up', ['productId' => $model->product_id]);
        return $this->response()->success('操作成功')->refresh();
    }

    public function dialog(): void
    {
        $this->confirm('确定上架吗?');
    }
}
